home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Controls / Visual Basic Controls.iso / vbcontrol / spchauto / data1.cab / Samples / Visual_C++ / SpellCheckerOCX / SpellCheckerDlg.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-07-16  |  6.3 KB  |  253 lines

  1. // SpellCheckerDlg.cpp : implementation file
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "SpellChecker.h"
  6. #include "SpellCheckerDlg.h"
  7.  
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif
  13.  
  14. /////////////////////////////////////////////////////////////////////////////
  15. // CAboutDlg dialog used for App About
  16.  
  17. class CAboutDlg : public CDialog
  18. {
  19. public:
  20.     CAboutDlg();
  21.  
  22. // Dialog Data
  23.     //{{AFX_DATA(CAboutDlg)
  24.     enum { IDD = IDD_ABOUTBOX };
  25.     //}}AFX_DATA
  26.  
  27.     // ClassWizard generated virtual function overrides
  28.     //{{AFX_VIRTUAL(CAboutDlg)
  29.     protected:
  30.     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  31.     //}}AFX_VIRTUAL
  32.  
  33. // Implementation
  34. protected:
  35.     //{{AFX_MSG(CAboutDlg)
  36.     //}}AFX_MSG
  37.     DECLARE_MESSAGE_MAP()
  38. };
  39.  
  40. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  41. {
  42.     //{{AFX_DATA_INIT(CAboutDlg)
  43.     //}}AFX_DATA_INIT
  44. }
  45.  
  46. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  47. {
  48.     CDialog::DoDataExchange(pDX);
  49.     //{{AFX_DATA_MAP(CAboutDlg)
  50.     //}}AFX_DATA_MAP
  51. }
  52.  
  53. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  54.     //{{AFX_MSG_MAP(CAboutDlg)
  55.         // No message handlers
  56.     //}}AFX_MSG_MAP
  57. END_MESSAGE_MAP()
  58.  
  59. /////////////////////////////////////////////////////////////////////////////
  60. // CSpellCheckerDlg dialog
  61.  
  62. CSpellCheckerDlg::CSpellCheckerDlg(CWnd* pParent /*=NULL*/)
  63.     : CDialog(CSpellCheckerDlg::IDD, pParent)
  64. {
  65.     //{{AFX_DATA_INIT(CSpellCheckerDlg)
  66.     m_edit = _T("");
  67.     m_static = _T("");
  68.     //}}AFX_DATA_INIT
  69.     // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  70.     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  71. }
  72.  
  73. void CSpellCheckerDlg::DoDataExchange(CDataExchange* pDX)
  74. {
  75.     CDialog::DoDataExchange(pDX);
  76.     //{{AFX_DATA_MAP(CSpellCheckerDlg)
  77.     DDX_Control(pDX, IDC_LIST1, m_listbox);
  78.     DDX_Control(pDX, IDC_SPELL1, m_SpellChecker);
  79.     DDX_Text(pDX, IDC_EDIT1, m_edit);
  80.     DDX_Text(pDX, IDC_STATIC_TEXT, m_static);
  81.     //}}AFX_DATA_MAP
  82. }
  83.  
  84. BEGIN_MESSAGE_MAP(CSpellCheckerDlg, CDialog)
  85.     //{{AFX_MSG_MAP(CSpellCheckerDlg)
  86.     ON_WM_SYSCOMMAND()
  87.     ON_WM_PAINT()
  88.     ON_WM_QUERYDRAGICON()
  89.     ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
  90.     ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
  91.     ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
  92.     ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
  93.     ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
  94.     //}}AFX_MSG_MAP
  95. END_MESSAGE_MAP()
  96.  
  97. /////////////////////////////////////////////////////////////////////////////
  98. // CSpellCheckerDlg message handlers
  99.  
  100. BOOL CSpellCheckerDlg::OnInitDialog()
  101. {
  102.     CDialog::OnInitDialog();
  103.  
  104.     // Add "About..." menu item to system menu.
  105.  
  106.     // IDM_ABOUTBOX must be in the system command range.
  107.     ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  108.     ASSERT(IDM_ABOUTBOX < 0xF000);
  109.  
  110.     CMenu* pSysMenu = GetSystemMenu(FALSE);
  111.     if (pSysMenu != NULL)
  112.     {
  113.         CString strAboutMenu;
  114.         strAboutMenu.LoadString(IDS_ABOUTBOX);
  115.         if (!strAboutMenu.IsEmpty())
  116.         {
  117.             pSysMenu->AppendMenu(MF_SEPARATOR);
  118.             pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  119.         }
  120.     }
  121.  
  122.     // Set the icon for this dialog.  The framework does this automatically
  123.     //  when the application's main window is not a dialog
  124.     SetIcon(m_hIcon, TRUE);            // Set big icon
  125.     SetIcon(m_hIcon, FALSE);        // Set small icon
  126.     
  127.     // TODO: Add extra initialization here
  128.     //CAutoCorrect *pAutoCorrect = (CAutoCorrect *)GetDlgItem( IDC_AUTOCORRECT1 );
  129.     return TRUE;  // return TRUE  unless you set the focus to a control
  130. }
  131.  
  132. void CSpellCheckerDlg::OnSysCommand(UINT nID, LPARAM lParam)
  133. {
  134.     if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  135.     {
  136.         CAboutDlg dlgAbout;
  137.         dlgAbout.DoModal();
  138.     }
  139.     else
  140.     {
  141.         CDialog::OnSysCommand(nID, lParam);
  142.     }
  143. }
  144.  
  145. // If you add a minimize button to your dialog, you will need the code below
  146. //  to draw the icon.  For MFC applications using the document/view model,
  147. //  this is automatically done for you by the framework.
  148.  
  149. void CSpellCheckerDlg::OnPaint() 
  150. {
  151.     if (IsIconic())
  152.     {
  153.         CPaintDC dc(this); // device context for painting
  154.  
  155.         SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  156.  
  157.         // Center icon in client rectangle
  158.         int cxIcon = GetSystemMetrics(SM_CXICON);
  159.         int cyIcon = GetSystemMetrics(SM_CYICON);
  160.         CRect rect;
  161.         GetClientRect(&rect);
  162.         int x = (rect.Width() - cxIcon + 1) / 2;
  163.         int y = (rect.Height() - cyIcon + 1) / 2;
  164.  
  165.         // Draw the icon
  166.         dc.DrawIcon(x, y, m_hIcon);
  167.     }
  168.     else
  169.     {
  170.         CDialog::OnPaint();
  171.     }
  172. }
  173.  
  174. // The system calls this to obtain the cursor to display while the user drags
  175. //  the minimized window.
  176. HCURSOR CSpellCheckerDlg::OnQueryDragIcon()
  177. {
  178.     return (HCURSOR) m_hIcon;
  179. }
  180.  
  181. void CSpellCheckerDlg::OnButton1() 
  182. {
  183.     // TODO: Add your control notification handler code here
  184.  
  185.    //get app path
  186.    TCHAR szExePath[MAX_PATH];
  187.    GetModuleFileName( AfxGetInstanceHandle(), szExePath, MAX_PATH );
  188.    
  189.    //remove app name from path
  190.    CString strPath = szExePath;
  191.    strPath = strPath.Left( strPath.ReverseFind('\\') );
  192.    strPath += "\\";
  193.  
  194.    // create path strings
  195.    CString strMainDictPath = strPath + "SpellUs.lex";
  196.    CString strUserDictPath = strPath + "custom.dic";
  197.  
  198.    if( FALSE == FileExists(strMainDictPath) )
  199.    {
  200.        AfxMessageBox(_T("Couldn't find spelling dictionary file !\n Make sure that SpellUs.lex file is in app path.") );
  201.    }
  202.  
  203.    m_SpellChecker.OpenDictionary( strMainDictPath, strUserDictPath );
  204.  
  205. }
  206.  
  207. void CSpellCheckerDlg::OnButton2() 
  208. {
  209.     // TODO: Add your control notification handler code here
  210.     m_SpellChecker.AboutBox();
  211. }
  212.  
  213. void CSpellCheckerDlg::OnButton3() 
  214. {
  215.     // TODO: Add your control notification handler code here
  216.     UpdateData(TRUE);
  217.     if( m_SpellChecker.IsWordExist(m_edit) )
  218.         m_static = "Exists";
  219.     else
  220.         m_static = "Doesn't Exist";
  221.     UpdateData(FALSE);
  222. }
  223.  
  224. void CSpellCheckerDlg::OnButton4() 
  225. {
  226.     // TODO: Add your control notification handler code here
  227.  
  228.     UpdateData(TRUE);
  229.     CString strSuggestion;
  230.     INT nCount = 0;
  231.     m_listbox.ResetContent();
  232.     do{
  233.      strSuggestion = m_SpellChecker.GetSuggestion(m_edit, nCount);
  234.      nCount++;
  235.      if (strSuggestion != "")
  236.          m_listbox.AddString(strSuggestion);
  237.     }while( !(strSuggestion == "") );
  238.     
  239. }
  240.  
  241. void CSpellCheckerDlg::OnButton5() 
  242. {
  243.     // TODO: Add your control notification handler code here
  244.     UpdateData(TRUE);
  245.    m_SpellChecker.AddWord(m_edit);
  246. }
  247.  
  248. BOOL CSpellCheckerDlg::FileExists( LPCSTR lszFile ) 
  249. {
  250.    CFileStatus fs;
  251.    return CFile::GetStatus( lszFile, fs );
  252. }
  253.